package com.traffic.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.traffic.entity.Score;
import com.traffic.mapper.ScoreMapper;
import com.traffic.service.ScoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService {

    @Autowired
    ScoreMapper scoreMapper;

    @Override
    public void saveScore(List<Double> Ascore, List<Double> Bscore, Double fscore,Long userid) {

        Score score = new Score();

        score.setCreatetime(new DateTime());
        score.setUserId(userid);
        score.setX1(Ascore.get(0));
        score.setX2(Ascore.get(1));
        score.setX3(Ascore.get(2));
        score.setX4(Ascore.get(3));
        score.setX5(Ascore.get(4));
        score.setX6(Ascore.get(5));
        score.setX7(Ascore.get(6));
        score.setX8(Ascore.get(7));

        score.setX9(Bscore.get(0));
        score.setX10(Bscore.get(1));
        score.setX11(Bscore.get(2));
        score.setX12(Bscore.get(3));
        score.setX13(Bscore.get(4));
        score.setX14(Bscore.get(5));
        score.setX15(Bscore.get(6));
        score.setX16(Bscore.get(7));
        score.setScore(fscore);

        scoreMapper.insert(score);

    }

    @Override
    public List<Score> queryByPhoneAndTime(String userid, String startDate, String endDate) throws ParseException {
        startDate = startDate.replace("Z"," UTC");
        endDate = endDate.replace("Z"," UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date stime = new Date();
        Date etime = new Date();

        if(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            stime = format.parse(startDate);
            etime = format.parse(endDate);
        }
        else{
            stime = format.parse("2020-01-14T00:00:00.000 UTC");
            etime = format.parse("2034-01-14T00:00:00.000 UTC");
        }
        final String sresult = defaultFormat.format(stime);
        final String eresult = defaultFormat.format(etime);

        QueryWrapper<Score> wrapper = new QueryWrapper<>();

        wrapper.eq("user_id",userid).and(StringUtils.isNotBlank(startDate)
                        && StringUtils.isNotBlank(endDate),
//                dateSql -> dateSql.ge("createtime", startDate.toString())
//                        .apply("createtime <= DATE_ADD({0}, INTERVAL 1 DAY)", endDate.toString()));
                dateSql ->dateSql.between("createtime",sresult,eresult)).orderByDesc("createtime");

        List<Score> scores = scoreMapper.selectList(wrapper);

        return scores;

    }
}
